package com.polaris.live.widget.marqueen

import android.content.Context
import android.view.View
import android.view.animation.Animation
import com.polaris.live.interface_bag.AnimationListenerAdapter

/**
 * 负责解析提供数据及事件监听
 *
 * @author xuexiang
 * @since 2019/1/14 下午10:05
 */
abstract class MarqueeFactory<T : View?, E : Any>(protected var mContext: Context) {

    private var mOnItemClickListener: OnItemClickListener<T, E>? = null
    private var mViews: MutableList<T>? = null
    private var mDatas: List<E>? = null
    private var isOnItemClickRegistered = false
    private var mMarqueeView: MarqueeView? = null
    private var mReplace: Boolean = false
    abstract fun generateMarqueeItemView(data: E): T
    fun setData(datas: List<E>?) {
        if (datas.isNullOrEmpty()) {
            return
        }
        if (mViews != null) {
            mReplace = true
        }
        mDatas = datas
        mViews = ArrayList()
        for (i in datas.indices) {
            val data = datas[i]
            val mView = generateMarqueeItemView(data)
            mViews?.add(mView)
        }
        registerOnItemClick()
        if (!mReplace) {
            mMarqueeView?.setMarqueeFactory(this)
        }
    }

    /**
     * 设置Item的监听
     *
     * @param onItemClickListener
     */
    fun setOnItemClickListener(onItemClickListener: OnItemClickListener<T, E>) {
        mOnItemClickListener = onItemClickListener
        registerOnItemClick()
    }

    val marqueeViews: List<T>?
        get() = mViews

    private fun registerOnItemClick() {
        if (mOnItemClickListener != null && mDatas != null) {
            for (i in mDatas!!.indices) {
                val view = mViews!![i]
                val data = mDatas!![i]
                val viewHolder = ViewHolder(view, data, i)
                view!!.setOnClickListener { view -> mOnItemClickListener!!.onItemClick(view, viewHolder) }
            }
            isOnItemClickRegistered = true
        }
    }

    /**
     * 条目点击监听
     *
     * @param <V>
     * @param <E>
    </E></V> */
    interface OnItemClickListener<V : View?, E> {

        /**
         * 条目点击
         *
         * @param view
         * @param holder
         */
        fun onItemClick(view: View?, holder: ViewHolder<V, E>?)
    }

    class ViewHolder<V : View?, P>(var view: V, var data: P, var position: Int) {

        fun setView(view: V): ViewHolder<*, *> {
            this.view = view
            return this
        }

        fun setData(data: P): ViewHolder<*, *> {
            this.data = data
            return this
        }

        fun setPosition(position: Int): ViewHolder<*, *> {
            this.position = position
            return this
        }
    }

    fun setAttachedToMarqueeView(marqueeView: MarqueeView) {
        mMarqueeView = marqueeView

        mMarqueeView?.animIn?.setAnimationListener(object : AnimationListenerAdapter() {
            override fun onAnimationEnd(animation: Animation) {
                mMarqueeView?.post { //一圈已经滑动完毕 更新数据
                    if (mMarqueeView?.displayedChild == 0) {
                        if (mReplace) {
                            mReplace = false
                            mMarqueeView?.setMarqueeFactory(this@MarqueeFactory)
                        }
                    }
                }
            }
        })
    }
}